#============================================================
#	Camera Limit By Map
#============================================================
# @datetime: 2022-3-15 00:34:43
#============================================================

## 设置镜头的有限的范围
@tool
class_name CameraLimit
extends Camera2DByTileMapBase


## 额外设置的范围
@export var margin : Rect2 = Rect2(0,0,0,0)


#============================================================
#   自定义
#============================================================
#(override)
func _update_camera():
	var camera : Camera2D = get_parent()
	var tilemap : TileMap = get_node_or_null(_tilemap)
	var rect : Rect2 = tilemap.get_used_rect()
	rect.position *= tilemap.cell_size
	rect.size *= tilemap.cell_size
#	Log.info(self, [ "相机范围", rect, ], '\t')
	
	rect.position += tilemap.global_position
	rect.end += tilemap.global_position
	
	camera.limit_left = rect.position.x + margin.position.x
	camera.limit_right = rect.end.x + margin.size.x
	camera.limit_top = rect.position.y + margin.position.y
	camera.limit_bottom = rect.end.y + margin.size.y


